<?php
/**
 * Created by PhpStorm.
 * User: 程阳
 * Date: 2017/2/27
 * Time: 21:12
 */

namespace Home\Controller;

use Common\Controller\HomeController;

class UserController extends HomeController
{
    // 用户中心
    public function index(){
        $headConfig = [
            'css' => ['main.min'],
            'js' => ['user.min'],
            'title' => '个人中心',
        ];
        $this->assign('headConfig',$headConfig);
        $this->display();
    }

    // 用户订单
    public function order(){
        $headConfig = [
            'css' => ['main.min'],
            'js' => ['user.min'],
            'title' => '我的订单',
        ];
        $this->assign('headConfig',$headConfig);

        $orderData = M("Order")->where("uid = {$_SESSION['home']['user']['uid']}")->select();
        $orderList = [];
        foreach($orderData as $k => $v){
            $orderList[$v['oid']] = M("OrderList")->where("oid={$v['oid']}")->select();
            foreach($orderList[$v['oid']] as $kk => $vv){
                $orderList[$v['oid']][$kk]['list'] = M("Goods")->where("gid={$orderList[$v['oid']][$kk]['gid']}")->getField("glist");
            }
        }
        $order = [];
        foreach($orderData as $k => $v){
            $order[] = [
                "order" => $v,
                "list" => $orderList[$v['oid']],
            ];
        }
//        dd($order);
        $this->assign("order",$order);
        $this->display();
    }

    public function login(){
        if(IS_POST){
            $uzhanghao = I("post.uzhanghao");
            $upassword = I("post.upassword");
            $res = M("User")->where("uzhanghao='{$uzhanghao}' and upassword='{$upassword}'")->find();
            if($res){
                $_SESSION['home']['user'] = $res;
                $this->success("登录成功",u('home/index/index'));die;
            }
        }
        $this->display();
    }

    public function loginOut(){
        $_SESSION['home']['user'] = NULL;
        $this->redirect('home/index/index');
    }

    public function register(){
        if(IS_POST){
            $data['uzhanghao'] = I("post.uzhanghao");
            $data['upassword'] = I("post.upassword");
            $icode = I("post.icode");
            if($this->check_verify($icode)){
                $res = M("User")->add($data);
                if($res){
                    $this->success("注册成功",u('home/user/index'));die;
                }else{
                    $this->error("注册失败");die;
                }
            }else{
                $this->error("验证码错误");die;
            }
        }
        $this->display();
    }

    public function code(){
        $this->verify();
    }

    public function editAddress(){
//        dd(I("post."));
        $data['uaname'] = I("post.uaname");
        $data['uaphone'] = I("post.uaphone");
        $data['uarea'] = I("post.uarea");
        $data['uadetail'] = I("post.uadetail");
        $data['uid'] = $_SESSION['home']['user']['uid'];
        $act = I("post.act");
        if($act == -1){
            $res = M("Uaddress")->add($data);
        }else{
            $data['uaid'] = intval($act);
            $res = M("Uaddress")->save($data);
            if($res){
                $res = $data['uaid'];
            }else{
                $this->ajaxReturn(['status'=>false]);
            }
        }
        if($res){
            $res = M("Uaddress")->where("uaid=$res")->find();
            $this->ajaxReturn(['status'=>true,'info'=>$res]);
        }else{
            $this->ajaxReturn(['status'=>false]);
        }
    }
}